package hu.uszeged.inf.stud.prf.todo.entity.task;

import hu.uszeged.inf.stud.prf.todo.entity.resource.Resource;
import hu.uszeged.inf.stud.prf.todo.entity.usertask.UserTask;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "task")
public class Task implements Serializable {
	
	private static final long serialVersionUID = -5635645173372982375L;

	@Id
	@SequenceGenerator(name = "idGenSeqTask", initialValue=5)
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "idGenSeqTask")
	private Long id;
	
	private String name;
		
	@ManyToMany(cascade = CascadeType.ALL)
	@JoinTable(name = "task_resource",
			joinColumns = @JoinColumn(name = "task_id"),
			inverseJoinColumns = @JoinColumn(name = "resource_id")
		)
	private List<Resource> resources;

	@OneToMany(mappedBy = "task")
	private List<UserTask> usertasks;
	
	public List<Resource> getResources() {
		return resources;
	}

	public void setResources(List<Resource> resources) {
		this.resources = resources;
	}

	public List<UserTask> getUserTasks() {
		return usertasks;
	}

	public void setUserTasks(List<UserTask> tasks) {
		this.usertasks = tasks;
	}
	
	public Task() {
		super();
	}

	public Task(Long id, String name, String descripton, List<Resource> resources) {
		super();
		this.id = id;
		this.name = name;
		this.resources = resources;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((resources == null) ? 0 : resources.hashCode());
		result = prime * result
				+ ((usertasks == null) ? 0 : usertasks.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Task other = (Task) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (resources == null) {
			if (other.resources != null)
				return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return "Task [id=" + id + ", name=" + name + ", resources=" + resources + "]";
	}
	
	
}
